﻿using MyNetwork;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NetWorkTest
{
	public partial class NetworkTestForm : Form
	{
		private void testIPs()
		{
			NetworkInterface[] nifs = NetworkInterface.GetAllNetworkInterfaces();
			foreach (NetworkInterface ni in nifs)
			{
				//if (ni.OperationalStatus == OperationalStatus.Up && ni.NetworkInterfaceType != NetworkInterfaceType.Loopback)
				if (ni.OperationalStatus == OperationalStatus.Up)
				{
					IPInterfaceProperties ipp = ni.GetIPProperties();
					foreach (UnicastIPAddressInformation ip in ipp.UnicastAddresses)
					{
						//if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
						//{
						//	System.Diagnostics.Debug.WriteLine(ip.Address.ToString());
						//}
						if (IPAddress.IsLoopback(ip.Address))
						{
							System.Diagnostics.Debug.WriteLine(ip.Address.ToString());
						}
					}
				}
			}

			System.Diagnostics.Debug.Write("testIPs");
		}
		public NetworkTestForm()
		{
			InitializeComponent();

			//testIPs();

			comboBox1.Items.Add(0);
			comboBox1.Items.Add(1);
			comboBox1.Items.Add(2);
			comboBox1.Items.Add(3);
			comboBox1.SelectedIndex = 0;


			comboBox2.Items.Add(0);
			comboBox2.Items.Add(1);
			comboBox2.SelectedIndex = 0;

			this.FormClosing += (s, e) =>
			  {
				  if (client.Connected) client.Close();
			  };

			client.Received += Client_Received;
		}

		private void Client_Received(string obj)
		{
			Invoke(new Action(() => listBox1.Items.Add(obj)));

			RVStatus rvRtn = new RVStatus("TEST");
			if (rvRtn.Parse(obj) == false) return;

			if (rvRtn.Command == rvEcho.Command)
			{
			}
			else if (rvRtn.Command == rvFeed.Command)
			{
			}
		}

		private void buttonConnection_Click(object sender, EventArgs e)
		{
			if (client.Connected) client.Close();
			else
			{
				if (string.IsNullOrEmpty(textBoxIP.Text))
				{
					textBoxIP.Focus();
					MessageBox.Show("Input Correct IP address");
					textBoxIP.SelectAll();
					return;
				}
				var ps = textBoxIP.Text.Split(new char[] { '.' });
				if (ps.Length < 4)
				{
					textBoxIP.Focus();
					MessageBox.Show("Input Correct IP address");
					textBoxIP.SelectAll();
					return;
				}

				try
				{
					Byte a = Convert.ToByte(ps[0]);
					Byte b = Convert.ToByte(ps[1]);
					Byte c = Convert.ToByte(ps[2]);
					Byte d = Convert.ToByte(ps[3]);
					if (a >= 225 || b > 255 || c > 255 || d > 255)
					{
						textBoxIP.Focus();
						MessageBox.Show("Input Correct IP address");
						textBoxIP.SelectAll();
						return;
					}

					int port = Convert.ToInt32(textBoxPort.Text);
					client.IP = textBoxIP.Text;
					client.Port = port;

					client.Connect();
				}
				catch(Exception ex)
				{
					textBoxIP.Focus();
					MessageBox.Show("Input Correct IP address" + Environment.NewLine + ex.Message);
					textBoxIP.SelectAll();
					return;
				}
			}
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			if (client.Connected)
			{
				if (buttonConnection.BackColor != Color.Green) buttonConnection.BackColor = Color.Green;
				if (!buttonSendEcho.Enabled) buttonSendEcho.Enabled = true;
			}
			else
			{
				if (buttonConnection.BackColor != SystemColors.Control) buttonConnection.BackColor = SystemColors.Control;
				if (buttonSendEcho.Enabled) buttonSendEcho.Enabled = false;
			}
		}

		private ClientSocketTcp client =new ClientSocketTcp();
		internal RVStatus rvEcho = new RVStatus("ECHO");
		internal RVStatus rvFeed = new RVStatus("Feed");
		//site:站点号 0,1,2,3

		internal RVStatus rvBin = new RVStatus("Bin");
		//site:站点号 0,1,2,3

		internal RVStatus rvSnap = new RVStatus("Snap");
		//site:站点号 0,1,2,3
		//identity:标识号0,1

		internal RVStatus rvAllInOne = new RVStatus("AllInOne");
		//site:站点号 0,1,2,3
		//identity:标识号0,1
		//count
		//全过程

		internal RVStatus rvConfig = new RVStatus("Config");
		//paramname = paramvalue

		private void buttonSendEcho_Click(object sender, EventArgs e)
		{
			client.Send(rvEcho.NewCommand());
		}
		private void buttonSendFeeding_Click(object sender, EventArgs e)
		{
			int sid = comboBox1.SelectedIndex;
			rvFeed.SiteID = sid;
			client.Send(rvFeed.NewCommand());
		}

		private void buttonBox_Click(object sender, EventArgs e)
		{
			int sid = comboBox2.SelectedIndex;
			rvBin.SiteID = sid;
			client.Send(rvBin.NewCommand());
		}

		private void buttonSnap_Click(object sender, EventArgs e)
		{
			int sid = comboBox1.SelectedIndex;
			int mid = comboBox2.SelectedIndex;
			int count = 0;
			try
			{
				count = Convert.ToInt32(textBoxCount.Text);
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, "Count Error" + Environment.NewLine + ex.Message);
				return;
			}

			rvSnap.SiteID = sid;
			rvSnap.MarkID1 = mid;
			rvSnap.Count1 = count;
			client.Send(rvSnap.NewCommand());
		}

		private void buttonAuto_Click(object sender, EventArgs e)
		{
			int count = 0;
			try
			{
				count = Convert.ToInt32(textBoxCount.Text);
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, "Count Error" + Environment.NewLine + ex.Message);
				return;
			}
			int sid = comboBox1.SelectedIndex;
			int mid = comboBox2.SelectedIndex;
			rvAllInOne.SiteID = sid;
			rvAllInOne.MarkID1 = mid;
			rvAllInOne.Count1 = count;

			client.Send(rvAllInOne.NewCommand());
		}

		private void buttonConfig_Click(object sender, EventArgs e)
		{
			try
			{
				string txt = textBoxParamName.Text + "=" + textBoxParamValue.Text;
				client.Send(rvConfig.NewCommand(txt));
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
	}
}
